package jcu.ict.profaid;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

@SuppressLint({ "NewApi", "SimpleDateFormat" })
public class FormEditQuestion extends Activity {
	@SuppressLint("NewApi")
	private static QuestionDbAdapter qdb;
	private static AnswerDbAdapter adb;
	private String course_id;
	private EditText edt_title;
	private EditText edt_question;
	private EditText edt_answer_1;
	private EditText edt_answer_2;
	private EditText edt_answer_3;
	private EditText edt_answer_4;
	private EditText edt_answer_5;
	private String questionID;
	private int countAnswer;
	private String edtTitle;
	private String date;
	private String edtQuestion;
	private String idEdtAsnwer1;
	private String idEdtAsnwer2;
	private String idEdtAsnwer3;
	private String idEdtAsnwer4;
	private String idEdtAsnwer5;
	private String i;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.form_add_question);

		Bundle bundle = this.getIntent().getExtras();

		course_id = bundle.getString("setInfoCourse");
		questionID = bundle.getString("setQuestionId");
		 i = (String) bundle.getCharSequence("setUserId");

		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		ColorDrawable colorDrawable = new ColorDrawable(
				Color.parseColor("#0099CC"));
		actionBar.setBackgroundDrawable(colorDrawable);

		edt_title = (EditText) findViewById(R.id.edtTitle);
		edt_question = (EditText) findViewById(R.id.edtQuestion);
		edt_answer_1 = (EditText) findViewById(R.id.edtAnswer1);
		edt_answer_2 = (EditText) findViewById(R.id.edtAnswer2);
		edt_answer_3 = (EditText) findViewById(R.id.edtAnswer3);
		edt_answer_4 = (EditText) findViewById(R.id.edtAnswer4);
		edt_answer_5 = (EditText) findViewById(R.id.edtAnswer5);

		qdb = new QuestionDbAdapter(getBaseContext());
		adb = new AnswerDbAdapter(getBaseContext());

		// display and update the question and answer
		qdb.open();
		adb.open();

		Cursor xCursor = qdb.populateQuestionEdit(questionID, course_id);
		xCursor.moveToFirst();

		edtTitle = xCursor.getString(0);
		edt_title.setText(edtTitle);

		edtQuestion = xCursor.getString(1);
		edt_question.setText(edtQuestion);

		Cursor aCursor = adb.PopulateEditAnswer(questionID);
		aCursor.moveToFirst();
		countAnswer = aCursor.getCount();
		if (aCursor.getCount() == 2) {
			edt_answer_1.setText(aCursor.getString(0));
			idEdtAsnwer1 = aCursor.getString(1);
			aCursor.moveToNext();

			edt_answer_2.setText(aCursor.getString(0));
			idEdtAsnwer2 = aCursor.getString(1);
		} else if (aCursor.getCount() == 3) {
			edt_answer_1.setText(aCursor.getString(0));
			idEdtAsnwer1 = aCursor.getString(1);
			aCursor.moveToNext();

			edt_answer_2.setText(aCursor.getString(0));
			idEdtAsnwer2 = aCursor.getString(1);
			aCursor.moveToNext();

			edt_answer_3.setText(aCursor.getString(0));
			idEdtAsnwer3 = aCursor.getString(1);
		} else if (aCursor.getCount() == 4) {
			edt_answer_1.setText(aCursor.getString(0));
			idEdtAsnwer1 = aCursor.getString(1);
			aCursor.moveToNext();

			edt_answer_2.setText(aCursor.getString(0));
			idEdtAsnwer2 = aCursor.getString(1);
			aCursor.moveToNext();

			edt_answer_3.setText(aCursor.getString(0));
			idEdtAsnwer3 = aCursor.getString(1);
			aCursor.moveToNext();

			edt_answer_4.setText(aCursor.getString(0));
			idEdtAsnwer4 = aCursor.getString(1);
		} else if (aCursor.getCount() == 5) {
			edt_answer_1.setText(aCursor.getString(0));
			idEdtAsnwer1 = aCursor.getString(1);
			aCursor.moveToNext();

			edt_answer_2.setText(aCursor.getString(0));
			idEdtAsnwer2 = aCursor.getString(1);
			aCursor.moveToNext();

			edt_answer_3.setText(aCursor.getString(0));
			idEdtAsnwer3 = aCursor.getString(1);
			aCursor.moveToNext();

			edt_answer_4.setText(aCursor.getString(0));
			idEdtAsnwer4 = aCursor.getString(1);
			aCursor.moveToNext();

			edt_answer_5.setText(aCursor.getString(0));
			idEdtAsnwer5 = aCursor.getString(1);
		}

		qdb.close();
		adb.close();

		Button btnSave = (Button) findViewById(R.id.btnCreate);
		Button btnClose = (Button) findViewById(R.id.btnCancel);
		
		btnSave.setText("Edit");

		btnSave.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				edtTitle = edt_title.getText().toString();
				edtQuestion = edt_question.getText().toString();

				String question = edtQuestion.replaceAll(",", " ");

				Calendar c = Calendar.getInstance();
				SimpleDateFormat df1 = new SimpleDateFormat("dd-MM-yyyy");
				date = df1.format(c.getTime());

				qdb.open();
				adb.open();

				if (qdb.updateTitle(questionID, course_id, edtTitle, question,
						date)) {
					if (countAnswer == 2) {
						if (adb.updateAnswer(idEdtAsnwer1, edt_answer_1
								.getText().toString())
								&& adb.updateAnswer(idEdtAsnwer2, edt_answer_2
										.getText().toString())) {
							Toast.makeText(getBaseContext(),
									"Update successful.", Toast.LENGTH_LONG)
									.show();
						}
					}
					if (countAnswer == 3) {
						if (adb.updateAnswer(idEdtAsnwer1, edt_answer_1
								.getText().toString())
								&& adb.updateAnswer(idEdtAsnwer2, edt_answer_2
										.getText().toString())
								&& adb.updateAnswer(idEdtAsnwer3, edt_answer_3
										.getText().toString())) {
							Toast.makeText(getBaseContext(),
									"Update successful.", Toast.LENGTH_LONG)
									.show();
						}
					}
					if (countAnswer == 4) {
						if (adb.updateAnswer(idEdtAsnwer1, edt_answer_1
								.getText().toString())
								&& adb.updateAnswer(idEdtAsnwer2, edt_answer_2
										.getText().toString())
								&& adb.updateAnswer(idEdtAsnwer3, edt_answer_3
										.getText().toString())
								&& adb.updateAnswer(idEdtAsnwer4, edt_answer_4
										.getText().toString())) {
							Toast.makeText(getBaseContext(),
									"Update successful.", Toast.LENGTH_LONG)
									.show();
						}
					}
					if (countAnswer == 4) {
						if (adb.updateAnswer(idEdtAsnwer1, edt_answer_1
								.getText().toString())
								&& adb.updateAnswer(idEdtAsnwer2, edt_answer_2
										.getText().toString())
								&& adb.updateAnswer(idEdtAsnwer3, edt_answer_3
										.getText().toString())
								&& adb.updateAnswer(idEdtAsnwer4, edt_answer_4
										.getText().toString())) {
							Toast.makeText(getBaseContext(),
									"Update successful.", Toast.LENGTH_LONG)
									.show();
						}
					}
					if (countAnswer == 5) {
						if (adb.updateAnswer(idEdtAsnwer1, edt_answer_1
								.getText().toString())
								&& adb.updateAnswer(idEdtAsnwer2, edt_answer_2
										.getText().toString())
								&& adb.updateAnswer(idEdtAsnwer3, edt_answer_3
										.getText().toString())
								&& adb.updateAnswer(idEdtAsnwer4, edt_answer_4
										.getText().toString())
								&& adb.updateAnswer(idEdtAsnwer5, edt_answer_5
										.getText().toString())) {
							Toast.makeText(getBaseContext(),
									"Update successful.", Toast.LENGTH_LONG)
									.show();
						}
					}

				} else {
					Toast.makeText(getBaseContext(), "Update failed.",
							Toast.LENGTH_LONG).show();
				}

				qdb.close();
				adb.close();

				Intent in = new Intent(FormEditQuestion.this, MainResult.class);
				Bundle bundle = new Bundle();
				bundle.putCharSequence("setQuestionId", questionID);
				bundle.putCharSequence("setInfoCourse", course_id);
				bundle.putCharSequence("setUserId", i);
				in.putExtras(bundle);
				startActivity(in);
				finish();
			}

		});

		btnClose.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent in = new Intent(FormEditQuestion.this, MainQuestion.class);
				Bundle bundle = new Bundle();
				bundle.putCharSequence("setInfoCourse", course_id);
				bundle.putCharSequence("setUserId", i);
				in.putExtras(bundle);
				startActivity(in);
				finish();
			}
		});
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// app icon in action bar clicked; go home
			Intent intent = new Intent(this, MainQuestion.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			Bundle bundle = new Bundle();
			bundle.putCharSequence("setInfoCourse", course_id);
			intent.putExtras(bundle);
			startActivity(intent);
			finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent i = new Intent(FormEditQuestion.this, MainQuestion.class);
			Bundle bundle = new Bundle();
			bundle.putCharSequence("setInfoCourse", course_id);
			i.putExtras(bundle);
			startActivity(i);
			finish();
		}
		return true;
	}
}
